const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
   
    init_requests = 0; //网络初始化请求次数
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //1.初始化
        this.initMgr();
    }

    async start () {
        //1.微信是否已经授权，授权了进入start场景，没授权生成授权按键
        var res = await cc.wx.get_userinfo();
        console.log("判断用户是否已经授权：",res);
        if(res.errMsg.indexOf(":fail") != -1)
        {
            console.log('用户未授权');            
            let button = window["wx"].createUserInfoButton({
                type: 'text',
                text: '进入游戏',
                style: {
                    left: window.innerWidth / 3.4,
                    bottom: window.innerHeight / 3,
                    width: window.innerWidth / 2.5,                   
                    height:50,
                    lineHeight: 50,
                    backgroundColor: '#ffbe06',
                    color: '#ffffff',
                    textAlign: 'center',
                    fontSize: 22,
                    borderRadius: 4,
                }
            })
            button.onTap(async (res) => {                
                if(res.errMsg == "getUserInfo:ok")
                {
                    console.log("用户授权成功");
                    button.hide();
                    cc.game_config.wx_encryptedData = res.encryptedData;
                    cc.game_config.wx_iv = res.iv;
                    cc.game_config.wx_name = res.userInfo.nickName;
                    cc.game_config.wx_image = res.userInfo.avatarUrl;
                    cc.game_config.wx_rawData = res.rawData;
                    cc.game_config.wx_signature = res.signature;
                    this.get_game_config();
                    
                }
                else
                {
                    console.log("用户拒绝了授权")
                    cc.wx.wx_tips("为了让您更好的游戏体验，请同意授权！",false)
                }               
            })           
        }
        else
        {
            console.log('用户已授权')
            cc.game_config.wx_encryptedData = res.encryptedData;
            cc.game_config.wx_iv = res.iv;
            cc.game_config.wx_name = res.userInfo.nickName;
            cc.game_config.wx_image = res.userInfo.avatarUrl;    
            cc.game_config.wx_rawData = res.rawData;
            cc.game_config.wx_signature = res.signature;                   
            this.get_game_config();
            
        }
        //2.是否需要加载资源
        //cc.wx.wx_loading('加载中')
    }

    initMgr(){
        //全局变量
        cc.vv = {}; 
        cc.game_config = {};
        //微信模块   
        cc.wx = this.node.getComponent('wx'); 

        cc.utils = this.node.getComponent('utils'); 
    }

    //游戏的后台配置
    async get_game_config(){
        var res = await cc.wx.get_code();
        console.log('code:',res);
        var body = {};
        body['channel_id'] = 10001;
        body['code'] = res.code;
        res = await cc.wx.http('/get/index/jscode2session/','post',body);       
        this.init_request(res.data.openid);
    }

    async init_request(openid){
        var body = {};
        body['openid'] = openid;
        body['channel_id'] = 10001;
        body['encryptedData'] = encodeURIComponent(cc.game_config.wx_encryptedData);
        body['iv'] = encodeURIComponent(cc.game_config.wx_iv);
        body['rawData'] = encodeURIComponent(cc.game_config.wx_rawData);
        body['signature'] = cc.game_config.wx_signature;
        cc.wx.wx_loading('加载中');
        var res = await cc.wx.http('get/index/init/','post',body);
        console.log('init返回：',res);
        if(res.data.code == 200)
        {            
            cc.wx.wx_close_loading();
            console.log('openid:',res);
            cc.game_config = res.data.game_config;            
            console.log("cc.game_config.uid:",cc.game_config.uid);
            cc.director.loadScene('start');
        }
        else
        {
            this.init_requests = this.init_requests + 1;
            if(this.init_requests <= 3)
            {
                cc.wx.wx_close_loading();
                this.init_request(openid);
            }
            else
            {
                cc.wx.wx_close_loading();
                cc.wx.wx_tips('网络繁忙');
            }            
        }        
    }

    update (dt) {

    }
}
